package com.ruoyi.factory.taxShare.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.taxShare.service.TaxHandService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: Liujiaji
 * @date: 2023/8/11 10:24
 * @description: TODO 税务平台业务处理类销项业务
 **/
@Controller("taxSharHandle")
@ResponseBody
@EnableScheduling
@RequestMapping("/api/taxSharHandle")
public class TaxHandController {

    @Autowired
    private TaxHandService taxHandService;

    /**
     * @author: Liujiaji
     * @date: 2023/8/11 10:26
     * @description: TODO 通过用友获取erp数据,数据处理之后推送给税务共享平台 销售发票
     * @param: [request, req]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/invoicePushxsfp")
    public AjaxResult invoicePushxsfp(@RequestBody String request, HttpServletRequest req) {
        String remoteAddr = req.getRemoteAddr();
        String localAddr = req.getLocalAddr();
        if (StringUtils.isNotEmpty(request)) {
            //如果request不为空则推送
            return taxHandService.invoicePushParamxsfp(request);
        }
        return null;
    }

    /**
     * @author: Liujiaji
     * @date: 2023/8/11 10:26
     * @description: TODO 通过用友获取erp数据,数据处理之后推送给税务共享平台 内部结算清单结算
     * @param: [request, req]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/invoicePushjsqd")
    public AjaxResult invoicePushjsqd(@RequestBody String request, HttpServletRequest req) {
        String remoteAddr = req.getRemoteAddr();
        String localAddr = req.getLocalAddr();
        if (StringUtils.isNotEmpty(request)) {
            //如果request不为空则推送
            return taxHandService.invoicePushParamjsqd(request);
        }
        return null;
    }

    /**
     * @author: Liujiaji
     * @date: 2023/8/11 10:24
     * @description: TODO 税务平台回调更新单据数据
     * @param: [req, request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/taxHandNotify")
    public AjaxResult taxUpDataBill(@RequestBody String req, HttpServletRequest request) {
        JSONObject jsonObject = JSONObject.fromObject(req);
        //根据单据号和单据类型回写四个
        return taxHandService.taxUpDataBill(jsonObject, request);
    }

    @PostMapping("/test")
    public AjaxResult test(@RequestBody String req, HttpServletRequest request) {
        JSONObject jsonObject = JSONObject.fromObject(req);
        //根据单据号和单据类型回写四个
        return taxHandService.test(jsonObject, request);
    }

    //清空防止多次回调id
    public void deletenotifyall() {
        taxHandService.deletenotifyall();
    }

    /**
     * @author: LiuJiaji
     * @date: 2025/5/16 16:22
     * @description: TODO 客户信息推送到金税平台 定时任务推送一天推送两次 中午12点多推送一次,晚上6点多推送一次
     * @param:
     * @return:
     **/
    @PostMapping("/pushCustomer")
    public void pushCustomer(String times, String timee){
        if(StringUtils.isBlank(times) || StringUtils.isBlank(timee)){
            Calendar instance = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date time = instance.getTime();
            timee = sdf.format(time);
            instance.add(Calendar.DATE,-7);
            Date time1 = instance.getTime();
            times = sdf.format(time1);
        }
        taxHandService.pushCustomer(times,timee);
    }

}
